﻿using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Core.Dri
{
    public class TeacherBll : ExampleBase<DriTeacher, TeacherBll>
    {
        /// <summary>
        /// 排序实体
        /// </summary>
        public IQueryable<DriTeacher> SortEntities
        {
            get { return Entities.OrderByDescending(m => m.Num); }  //根据流水号进行排序
        }

        private const string NumRule = "QCTEA";  //定义流水号规则（此处暂时作为常量，后续将写入到系统设置中）

        /// <summary>
        /// 产生新的流水号
        /// </summary>
        public string NewNum
        {
            get
            {
                string result = string.Empty;
                var maxNumEntity = Entities.OrderByDescending(m => m.Num).FirstOrDefault();
                if (maxNumEntity != null)
                {
                    string maxNum = maxNumEntity.Num;
                    result = string.Format("{0}{1}", NumRule, (Convert.ToInt32(maxNum.Substring(NumRule.Length)) + 1).ToString().PadLeft(6, '0'));
                }
                else
                {
                    result = string.Format("{0}{1}", NumRule, "1".PadLeft(6, '0'));
                }
                return result;
            }
        }

        /// <summary>
        /// 根据ID获取名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetNameByID(string id)
        {
            string result = string.Empty;
            var teacher = Entities.FirstOrDefault(m => m.ID == id);
            if (teacher != null)
            {
                result = teacher.Name;
            }
            return result;
        }
    }
}
